package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.manor.ManorHarvestData;
import com.yanqu.road.entity.manor.ManorRankActivityData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/5/19 11:18
 */
public class UserManorHarvestDataDaoImpl extends TempDataObjectDao<ManorHarvestData> {
    public static String insertSql = "insert into t_u_manor_harvest(userId,harvestType,outPutId,lv,specialSkillLv,skinId,collectTime,realCollectTime,yieldStash,nickName,changeNameTimes,extendParam,baseSpeed,specialSkillAddRatio,otherAddRatio,helpAddRatio,rankActivityDataMap) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ManorHarvestData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHarvestType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOutPutId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSpecialSkillLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSkinId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCollectTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRealCollectTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getYieldStash()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getNickName()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChangeNameTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getExtendParam()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBaseSpeed()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSpecialSkillAddRatio()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOtherAddRatio()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getHelpAddRatio()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getRankActivityDataMap())));
        return getDb().executeNoneQuery(insertSql, para) > 0;
    }

    public static String updateSql = "update t_u_manor_harvest set lv=?,specialSkillLv=?,skinId=?,collectTime=?,realCollectTime=?,yieldStash=?,nickName=?,changeNameTimes=?,extendParam=?,baseSpeed=?,specialSkillAddRatio=?,otherAddRatio=?,helpAddRatio=?,rankActivityDataMap=? where userId=? and outPutId=?";

    @Override
    protected boolean updateData(ManorHarvestData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSpecialSkillLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSkinId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCollectTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRealCollectTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getYieldStash()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getNickName()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChangeNameTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getExtendParam()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBaseSpeed()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSpecialSkillAddRatio()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOtherAddRatio()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getHelpAddRatio()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getRankActivityDataMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOutPutId()));
        return getDb().executeNoneQuery(updateSql, para) > 0;
    }

    @Override
    protected ManorHarvestData parseData(ResultSet rs) throws SQLException {
        ManorHarvestData data = new ManorHarvestData();
        data.setUserId(rs.getLong("userId"));
        data.setHarvestType(rs.getInt("harvestType"));
        data.setOutPutId(rs.getInt("outPutId"));
        data.setLv(rs.getInt("lv"));
        data.setSpecialSkillLv(rs.getInt("specialSkillLv"));
        data.setSkinId(rs.getInt("skinId"));
        data.setCollectTime(rs.getLong("collectTime"));
        data.setRealCollectTime(rs.getLong("realCollectTime"));
        data.setYieldStash(rs.getLong("yieldStash"));
        data.setNickName(rs.getString("nickName"));
        data.setChangeNameTimes(rs.getInt("changeNameTimes"));
        data.setExtendParam(rs.getString("extendParam"));
        data.setBaseSpeed(rs.getLong("baseSpeed"));
        data.setSpecialSkillAddRatio(rs.getLong("specialSkillAddRatio"));
        data.setOtherAddRatio(rs.getLong("otherAddRatio"));
        data.setHelpAddRatio(rs.getLong("helpAddRatio"));
        String rsString = rs.getString("rankActivityDataMap");
        if (rsString == null || rsString.isEmpty()) {
            data.setRankActivityDataMap(new ConcurrentHashMap<>());
        } else {
            data.setRankActivityDataMap(JSONObject.parseObject(rsString, new TypeReference<Map<Integer, ManorRankActivityData>>() {}));
        }
        return data;
    }

    public static String getSql = "select * from t_u_manor_harvest where userId=?";

    public Map<Integer, ManorHarvestData> getHarvestDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ManorHarvestData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                ManorHarvestData harvestData = parse(rs);
                map.put(harvestData.getOutPutId(), harvestData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
